package com.caozhi.mspeoject.nb_message.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caozhi.mspeoject.nb_message.mapper.MediaMapper;
import com.caozhi.mspeoject.nb_message.mapper.MessageMapper;
import com.caozhi.mspeoject.nb_message.remote.EsMessageRemote;
import com.woniuxy.boot.common.bean.EsMessage;
import com.woniuxy.boot.common.bean.Media;
import com.woniuxy.boot.common.bean.Message;
import com.woniuxy.boot.common.dto.EsMessageDTO;
import com.woniuxy.boot.common.pojo.ResponseResult;

import com.woniuxy.boot.common.util.EntityConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("user")
@RequestMapping("user/message")
public class MessageController {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private EsMessageRemote esMessageRemote;
    @Autowired
    private MediaMapper mediaMapper;
    @GetMapping("getMessageById")
    public ResponseResult<EsMessage> getMessageById(@RequestParam("id") Long id) {
        EsMessageDTO esMessageDTO = new EsMessageDTO();
        esMessageDTO.setId(id);
        EsMessage esMessages = esMessageRemote.getMessages(esMessageDTO).getFirst();
        if(esMessages == null) {
            Message message  = messageMapper.selectById(id);
            EsMessage convert = EntityConverter.convert(message, EsMessage.class);
            LambdaQueryWrapper<Media> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Media::getMedia, message.getId());
            List<Media> media = mediaMapper.selectList(queryWrapper);
            convert.setMediaList(media);
            return new ResponseResult<>(convert);
        }
        return new ResponseResult<>(esMessages);
    }

    @GetMapping("getMessageByCategoryId")
    public ResponseResult<List<EsMessage>> getMessageByCategoryId(@RequestParam("categoryId") Long categoryId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<Message> messages = messageMapper.selectList(queryWrapper);
        List<EsMessage> esMessages = new ArrayList<>();
        messages.forEach(esMessage -> {
            EsMessage esMessage1 = EntityConverter.convert(esMessage, EsMessage.class);
            QueryWrapper<Media> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("message_id", esMessage1.getId());
           List<Media> media =  mediaMapper.selectList(queryWrapper1);
           esMessage1.setMediaList(media);
           esMessages.add(esMessage1);
        });
        return new ResponseResult<>(esMessages);
    }
}
